//
//  CNTVTabbarView.m
//  CNTV_iPhone
//
//  Created  on 3/4/12.
//  Copyright (c) 2012 CNTV. All rights reserved.
//


#import "CommonTabbarView.h"
#import "CommonTabbarController.h"


@implementation CommonTabbarView
@synthesize tabbarctrl = _tabbarctrl;
@synthesize lastSelectedBtn = _lastSelectedBtn;

-(void)dealloc{
    self.lastSelectedBtn = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImageView *bgImv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [bgImv setImage:[UIImage imageNamed:@"topbg.png"]];
        [self addSubview:bgImv];
        [bgImv release];
    }
    return self;
}

/**
 初始化tab按键
 */
- (void)initCtrl
{	
    NSArray *array = self.tabbarctrl.viewControllers;
	NSInteger width = self.frame.size.width/array.count;

	CGRect frame = CGRectMake(0, 0, width, self.frame.size.height);
    
    for(int i = 0; i < [array count]; i++){
        UIButton *bt = [[UIButton alloc] initWithFrame:frame];
		bt.backgroundColor = [UIColor clearColor];
        [bt setBackgroundImage:[UIImage imageNamed:@"topbg.png"] forState:UIControlStateSelected];
        bt.tag = i;
		 switch (i) {
            case 0://
                 bt.selected = YES;
                 self.lastSelectedBtn = bt;
                 [bt setImage:[UIImage imageNamed:@"home1.png"] forState:UIControlStateNormal];
                 [bt setImage:[UIImage imageNamed:@"home1.png"] forState:UIControlStateSelected];
                break;
            case 1://
                [bt setImage:[UIImage imageNamed:@"location1.png"] forState:UIControlStateNormal];
                [bt setImage:[UIImage imageNamed:@"location1.png"] forState:UIControlStateSelected];
                break; 
            case 2://
                [bt setImage:[UIImage imageNamed:@"commit1.png"] forState:UIControlStateNormal];
                [bt setImage:[UIImage imageNamed:@"commit1.png"] forState:UIControlStateSelected];
                break; 
            case 3://
                [bt setImage:[UIImage imageNamed:@"category1.png"] forState:UIControlStateNormal];
                [bt setImage:[UIImage imageNamed:@"category1.png"] forState:UIControlStateSelected];
                break; 
            case 4://
                [bt setImage:[UIImage imageNamed:@"setting1.png"] forState:UIControlStateNormal];
                [bt setImage:[UIImage imageNamed:@"setting1.png"] forState:UIControlStateSelected];
                break; 
            default:
                break;
        }
		//bt.showsTouchWhenHighlighted = YES;
		[bt addTarget:self action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:bt];
		[bt release];
		frame.origin.x += width;
    }
    
}
/**
 通过index选择特定的tab
 */
-(void)selectTabByIndex:(NSInteger)prmIndex{
    for (UIView *aView in self.subviews) {
        if ([aView isKindOfClass:[UIButton class]]) {
            if (aView.tag == prmIndex) {
                [self switchView:(UIButton *)aView];
                break;
            }
        }
    }
}

/**
 切换tab controller
 */
- (void)switchView:(UIButton *)sender
{
    if (self.lastSelectedBtn != sender) {
        self.tabbarctrl.selectedIndex = sender.tag;
        self.lastSelectedBtn.selected = NO;
        self.lastSelectedBtn = sender;
        sender.selected = !sender.selected;
    }
    
}


@end












